package com.sd.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.alex.books.BaseBook;
import com.alex.books.BookMark;
import com.alex.books.ReaderingInfo;
import com.alex.database.BookDao;
import com.alex.database.BookDatabaseHelper.Bookmark_TB;
import com.alex.database.BookDatabaseHelper.Chapter_TB;
import com.sd.view.BookMarkAdpter;
import com.sd.view.ChapterAdpter;


public class SdFirstProjectActivity extends Activity implements OnItemClickListener, OnItemLongClickListener {
    /** Called when the activity is first created. */
	
	public static final String BOOKDATA  = "bookdata"; 
	private ListView mChpaterLV;
	private ListView mBookMarkLV;
	private Cursor c;
	private TabHost tabhost;
	private ChapterAdpter chapterAdpter;
	private BookMarkAdpter bookMarkAdpter;
	private BaseBook book;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.chapter_activity);
        
        
        book = (BaseBook)this.getIntent().getSerializableExtra(BOOKDATA);
        tabhost = (TabHost)findViewById(R.id.tabhost);
        tabhost.setup();
        tabhost.addTab(tabhost.newTabSpec("tab1")     
                .setIndicator("TAB 1")     
                .setContent(R.id.tab1));     
        tabhost.addTab(tabhost.newTabSpec("tab2")     
                .setIndicator("TAB 2")     
                .setContent(R.id.tab2));     
        
        mChpaterLV = (ListView)findViewById(R.id.chpater_list);
        c = BookDao.getInstance().queryChapter(this,book.mBookId);
        chapterAdpter = new ChapterAdpter(this, c);
        mChpaterLV.setAdapter(chapterAdpter);
        mChpaterLV.setOnItemClickListener(this);
        mChpaterLV.setOnItemLongClickListener(this);
        
        Cursor c0 = BookDao.getInstance().queryBookMarks(this,book.mBookId);
        bookMarkAdpter = new BookMarkAdpter(this,c0);
        mBookMarkLV = (ListView)findViewById(R.id.bookmark_list);
        mBookMarkLV.setAdapter(bookMarkAdpter);
        mBookMarkLV.setOnItemClickListener(this);
        mBookMarkLV.setOnItemLongClickListener(this);
       /* GenerationContents genera = new GenerationContents(this);
        genera.openBook("/sdcard/sd/test.txt");*/
    }
    
	public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
		// TODO Auto-generated method stub
		if(parent.getId() == mChpaterLV.getId()){
			ReaderingInfo readeringInfo = new ReaderingInfo();
			Cursor c = (Cursor)parent.getItemAtPosition(position);
			readeringInfo.mCurChapter.id = c.getInt(c.getColumnIndex(Chapter_TB._ID));
			readeringInfo.mCurChapter.mChapterIndex = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_INDEX));
			readeringInfo.mCurChapter.mChapterName = c.getString(c.getColumnIndex(Chapter_TB.CHAPTER_NAME));
			readeringInfo.mCurChapter.mPosBegin= c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_BEGINPOS));
			readeringInfo.mCurChapter.mPosEnd = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_ENDPOS));
			readeringInfo.mCurChapter.mLength = c.getInt(c.getColumnIndex(Chapter_TB.CHAPTER_LENGTH));
			readeringInfo.mIndex = readeringInfo.mCurChapter.mChapterIndex;
			readeringInfo.mMarkPos = readeringInfo.mCurChapter.mPosBegin;
			c.close();
			
			Intent intent = new Intent();
			intent.putExtra(ReaderActivity.READINGINFO, readeringInfo);
			setResult(RESULT_OK,intent);
			finish();
		}else if(parent.getId() == mBookMarkLV.getId()){
			ReaderingInfo readeringInfo = new ReaderingInfo();
			Cursor c = (Cursor)parent.getItemAtPosition(position);
			readeringInfo.mCurChapter.id = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_ID));
			readeringInfo.mCurChapter.mChapterName = c.getString(c.getColumnIndex(Bookmark_TB.CHAPTER_NAME));
			readeringInfo.mCurChapter.mPosBegin = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_BEGIN));
			readeringInfo.mCurChapter.mPosEnd = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_END));
			readeringInfo.mCurChapter.mLength = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_LENGTH));
			readeringInfo.mIndex = c.getInt(c.getColumnIndex(Bookmark_TB.CHAPTER_INDEX));
			readeringInfo.mMarkPos = c.getInt(c.getColumnIndex(Bookmark_TB.BOOKMARK_MARK));
			c.close();
			
			Intent intent = new Intent();
			intent.putExtra(ReaderActivity.READINGINFO, readeringInfo);
			setResult(RESULT_OK,intent);
			finish();
		}
		
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		if(parent.getId() == mBookMarkLV.getId()){
			BookMark bookMark = new BookMark();
			Cursor c = (Cursor)parent.getItemAtPosition(position);
			bookMark.id = c.getInt(c.getColumnIndex(Bookmark_TB._ID));
			c.close();
			BookDao.getInstance().deleteBookMarks(this, bookMark.id);
			Cursor c0 = BookDao.getInstance().queryBookMarks(this,book.mBookId);
			bookMarkAdpter.changeCursor(c0);
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		/*chapterAdpter.getCursor().close();
		bookMarkAdpter.getCursor().close();*/
		
		super.onDestroy();
	}
    
	
}